package com.zb.rebbitMq;

import java.io.IOException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Envelope;

public class Consumer {
	private static final String QUEUE_NAME ="binbin";
	private static final ConnectionFactory factory = new ConnectionFactory();
	
	public Consumer() {
		//连接工厂
		
		//配置登陆账号
		factory.setHost("106.12.222.85");
		factory.setPort(5672);
		factory.setUsername("guest");
		factory.setPassword("guest");
		factory.setVirtualHost("/");
		
	}
	
	public void receive() {
		try {
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		
		//申明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println(consumerTag);
				System.out.println(envelope.getExchange());
				String message =new String(body,"utf-8");
				System.out.println("【recevice】"+message);
			}
			
		}; 
		
		channel.basicConsume(QUEUE_NAME, true, defaultConsumer);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
